package xin.printk.android;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import xin.printk.android.utils.AppConstants;
import xin.printk.android.utils.LogUtils;
import xin.printk.android.utils.PrefUtils;

public abstract class BasicFragment extends Fragment {

    protected View mView;

    public <T extends View> T findView(@IdRes int id) {
        return mView.findViewById(id);
    }

    protected abstract int bindLayoutResId();

    protected abstract void onInit();

    protected abstract void onExecute(Bundle savedInstanceState);

    public int getThemeColor() {
        return PrefUtils.getInt(AppConstants.THEME_COLOR, getResources().getColor(R.color.blue_light));
    }

    public String getThemeName() {
        return PrefUtils.getString(AppConstants.THEME_COLOR_NAME, getString(R.string.official_blue));
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.d(getTag() + " onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LogUtils.d(getTag() + " onCreateView");
        mView = inflater.inflate(bindLayoutResId(), container, false);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LogUtils.d(getTag() + " onViewCreated");
        onInit();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtils.d(getTag() + " onActivityCreated");
        onExecute(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        LogUtils.d(getTag() + " onAttach");
    }

    @Override
    public void onAttachFragment(Fragment childFragment) {
        super.onAttachFragment(childFragment);
        LogUtils.d(getTag() + " onAttachFragment");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.d(getTag() + " onActivityResult");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        LogUtils.d(getTag() + " onSaveInstanceState");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtils.d(getTag() + " onDestroyView");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LogUtils.d(getTag() + " onDetach");
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtils.d(getTag() + " onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtils.d(getTag() + " onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtils.d(getTag() + " onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtils.d(getTag() + " onStop");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.d(getTag() + " onDestroy");
    }
}
